package cn.tramy.pinxian.task.application.api

import cn.tramy.pinxian.common.util.*
import cn.tramy.pinxian.task.application.command.CreateTask
import cn.tramy.pinxian.task.application.command.FinishTask
import cn.tramy.pinxian.task.query.dao.TaskEntryMapper
import cn.tramy.pinxian.wind.command.CommandBus
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import reactor.core.publisher.Mono


@RestController
@RequestMapping("/task")
class TaskApi {

    companion object {
        private val LOGGER = logger(TaskApi::class.java)
    }

    @Autowired
    private lateinit var commandBus: CommandBus

    @Autowired
    private lateinit var taskEntryMapper: TaskEntryMapper

    @PostMapping("/new")
    fun newTask(content: String, timer: String?): Mono<Result<Any>> = monoResult {
        this.commandBus.send(CreateTask(content, timer))
    }

    @PostMapping("/finish")
    fun finish(uid: String) = monoResult {
        this.commandBus.send(FinishTask(uid))
    }

    @GetMapping("/{uid}")
    fun queryById(@PathVariable("uid") uid: String) = monoQueryResult { this.taskEntryMapper.queryById(uid) }


    @GetMapping("/all")
    fun queryAll() =
            monoQueryPageResult { LOGGER.debug("query all");this.taskEntryMapper.queryAll() }

}
